
//
//  HDComposeTextView.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/18.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit
let  HDPlaceholderLabelFont:CGFloat = 18
class HDComposeTextView: UITextView {
//占位文字
    var placeholderText :String?{
        didSet {
            placeholderLabel.text = placeholderText
            placeholderLabel.sizeToFit()
        }
    }
//字体大小 重写系统  要覆盖
   override var font:UIFont? {
        didSet {
            placeholderLabel.font  = font
        }
    }
//MARK  -初始化方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        prepareUI()
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textViewDidChange:"), name:  UITextViewTextDidChangeNotification, object: self)
    }
    //回调通知
   @objc  private func textViewDidChange(notification: NSNotification){
        placeholderLabel.hidden = hasText()
        
    }
    //移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }


    //添加子控件和约束
    private func prepareUI(){
        addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(cellMargin)
        }
    }
    //MARK  -懒加载控件
    //占位text
    private var placeholderLabel: UILabel = {
        let  label  = UILabel()
        label.textColor = UIColor.redColor()
        return label
    }()

}
